package com.michael.ems.controller;

import com.michael.ems.controller.req.AddExceptionReq;
import com.michael.ems.controller.req.DeleteExceptionReq;
import com.michael.ems.controller.req.ModifyExceptionReq;
import com.michael.ems.controller.req.QueryExceptionCondition;
import com.michael.ems.controller.vo.CodeVO;
import com.michael.ems.model.QException;
import com.michael.ems.model.QSystem;
import com.michael.io.ReqPage;
import com.michael.io.RespData;
import com.michael.io.RespPageBody;
import com.michael.io.RespStatus;
import com.michael.plugins.PageHelper;
import com.querydsl.core.types.Projections;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.SQLQueryFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
//todo 待权限校验
@Api(tags = "异常码管理模块")
@RestController
@RequestMapping(value = "/exp")
public class ExceptionController {

    @Autowired
    SQLQueryFactory sqlQueryFactory;
    QException exception = QException.exception;
    QSystem system = QSystem.system;

    @ApiOperation(value = "查看异常码")
    @RequestMapping(value = "/code/view", method = RequestMethod.GET)
    public RespData<CodeVO> viewExpCode(@RequestParam("sysId") Integer sysId,
                                        @RequestParam("code") String code) {
        CodeVO expCode = sqlQueryFactory.select(Projections.bean(CodeVO.class,
                exception.id, system.name.as("system"), exception.code,
                exception.desc, exception.content, exception.language,
                exception.createTime, exception.creator, exception.modifyTime, exception.modifier))
                .from(exception).innerJoin(system).on(system.id.eq(exception.sysId))
                .where(system.id.eq(sysId).and(exception.code.eq(code)))
                .fetchOne();
        RespData<CodeVO> result = new RespData<CodeVO>();
        result.setBody(expCode);
        return result;
    }

    @ApiOperation(value = "查询异常码")
    @RequestMapping(value = "/code/query", method = RequestMethod.POST)
    public RespPageBody<CodeVO> queryExpCode(
            @RequestBody @Valid ReqPage<QueryExceptionCondition> req) {
        QueryExceptionCondition condition = req.getCondition();
        SQLQuery<CodeVO> sqlQuery = sqlQueryFactory.select(Projections.bean(CodeVO.class,
                exception.id, this.system.name.as("system"), exception.code,
                exception.desc, exception.content, exception.language,
                exception.createTime, exception.creator, exception.modifyTime, exception.modifier))
                .from(exception).innerJoin(this.system).on(this.system.id.eq(exception.sysId));
        if (condition.getSysId() != null) {
            sqlQuery.where(system.id.eq(condition.getSysId()));
        }
        if (StringUtils.isNotBlank(condition.getCode())) {
            sqlQuery.where(exception.code.like(condition.getCode()));
        }
        if (StringUtils.isNotBlank(condition.getLanguage())) {
            sqlQuery.where(exception.language.eq(condition.getLanguage()));
        }
        PageHelper.buildPage(req.getPage());
        PageHelper.execute(sqlQuery);
        return PageHelper.getResp();
    }

    @ApiOperation(value = "新增异常码")
    @RequestMapping(value = "/code/addition", method = RequestMethod.POST)
    public RespStatus addExpCode(@RequestBody @Valid AddExceptionReq req) {
        sqlQueryFactory.insert(exception)
                .set(exception.sysId, req.getSysId())
                .set(exception.code, req.getCode())
                .set(exception.language, req.getLanguage())
                .set(exception.desc, req.getDesc())
                .set(exception.content, req.getContent())
                .execute();
        //todo 待用户信息填入
        return new RespStatus();
    }

    @ApiOperation(value = "修改异常码")
    @RequestMapping(value = "/code/modification", method = RequestMethod.POST)
    //todo 待权限校验
    public RespStatus modifyExpCode(@RequestBody ModifyExceptionReq req) {
        sqlQueryFactory.update(exception)
                .set(exception.code, req.getCode())
                .set(exception.language, req.getLanguage())
                .set(exception.desc, req.getDesc())
                .set(exception.content, req.getContent())
                .where(exception.id.eq(req.getId()))
                .execute();
        //todo 待用户信息填入
        return new RespStatus();
    }

    @ApiOperation(value = "删除异常码")
    @RequestMapping(value = "/code/deletion", method = RequestMethod.DELETE)
    public RespStatus deleteExpCode(@RequestBody DeleteExceptionReq req) {
        sqlQueryFactory.delete(exception)
                .where(exception.id.eq(req.getId()))
                .execute();
        return new RespStatus();
    }
}
